package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/handler/resp"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"github.com/share309/gojwt"
	"time"
)

func SendSms(c *gin.Context) {
	var req request.SendSmsReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "验证失败", err.Error())
		return
	}
	sms, err := config.SeckillClient.SendSms(c, &__.SendSmsReq{Mobile: req.Mobile})
	if err != nil {
		resp.Error(c, 400, "发送短信失败", nil)
	}
	resp.Success(c, "发送短信成功", sms)
}
func LoginUser(c *gin.Context) {
	var req request.LoginUserReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "验证失败", err.Error())
		return
	}
	user, err := config.SeckillClient.LoginUser(c, &__.LoginUserReq{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		resp.Error(c, 400, "用户登录失败", nil)
		return
	}
	token, err := gojwt.NewJWT(config.JWT_KEY).CreateToken(gojwt.CustomClaims{
		ID: uint(user.UserId),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400*3,
		},
	})
	if err != nil {
		resp.Error(c, 400, "token生成失败", nil)
		return
	}
	resp.Success(c, "用户登录成功", token)
}
